package org.springleaf.core.mvc.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springleaf.core.mvc.view.AbstractViewRender;
import org.springleaf.utils.WebHelper;

/**
 * Controller基类
 * @author leaf
 */
@SuppressWarnings("unchecked")
public abstract class BaseController extends AbstractViewRender{
	
	public static Log log = LogFactory.getLog("springleaf");
	
	/**
	 * 得到Request
	 */
	public HttpServletRequest getReq(){
		return WebHelper.getReq();
	}
	
	/**
	 * 得到session
	 */
	public HttpSession getSes(){
		return getReq().getSession();
	}
	
	/**
	 * 得到Session中name对应的Attribute
	 */
	public <T>T getSesAttr(String name){
		return (T) getSes().getAttribute(name);
	}
	
	/**
	 * 往Session的Attribute中放值
	 */
	public HttpSession setSesAttr(String name,Object v){
		HttpSession session = getSes();
		session.setAttribute(name, v);
		return session;
	}
	
	/**
	 * 删除Session中的Attribute
	 */
	public HttpSession removeSesAttr(String name){
		HttpSession session = getSes();
		session.removeAttribute(name);
		return session;
	}
	
	/**
	 * 得到Request中name对应的Attribute
	 */
	public <T>T getAttr(String name){
		return (T) getReq().getAttribute(name);
	}
	
	/**
	 * 往Request的Attribute中放值
	 */
	public HttpServletRequest setAttr(String name,Object v){
		HttpServletRequest request = getReq();
		getReq().setAttribute(name, v);
		return request;
	}
	
	/**
	 * 删除Request的Attribute中指定的数据
	 */
	public HttpServletRequest removeAttr(String name){
		HttpServletRequest request = getReq();
		getReq().removeAttribute(name);
		return request;
	}
	
	/**
	 * 得到Request paramster 中 name 的值
	 */
	public String getParam(String name){
		return getReq().getParameter(name);
	}
	
	/**
	 * 得到Request paramster 中 name 的值
	 */
	public String[] getParams(String name){
		return getReq().getParameterValues(name);
	}
	
}
